#!/usr/bin/env python2.7
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import sys, json

preamble = r'''/** \file
 *
 * \brief Automatically generated API method wrappers
 *
 * This file is automatically generated from a list of available routes on the
 * API server. Functions in this file take a remote object ID (when
 * appropriate) and optional arguments input_params and safe_to_retry.
 * input_params specifies the request payload to send to the server, and can be
 * supplied either as stringified JSON or a JSON object (if not provided, the
 * JSON of an empty dict will be sent). safe_to_retry specifies whether the
 * request is idempotent and can be retried.
 *
 * Each function returns the JSON that is returned by the API server.
 */

// ===> Do not modify this file by hand! <===
//
// This file is automatically generated by
// src/api_wrappers/generateCppAPIHWrappers.py. (Run make api_wrappers to
// update it.)

#ifndef DXCPP_API_H
#define DXCPP_API_H

#include "dxjson/dxjson.h"

namespace dx {'''

postscript = '''
}
#include "dxcpp.h"

#endif'''

class_method_template = '''
  JSON {method_name}(const std::string &input_params="{{}}", const bool safe_to_retry={to_retry});
  JSON {method_name}(const dx::JSON &input_params, const bool safe_to_retry={to_retry});'''

object_method_template = '''
  JSON {method_name}(const std::string &object_id, const std::string &input_params="{{}}", const bool safe_to_retry={to_retry});
  JSON {method_name}(const std::string &object_id, const dx::JSON &input_params, const bool safe_to_retry={to_retry});'''

# Overloads with alias are named differently to eliminate ambiguity between
# method(app_id_or_name, input_params) and method(app_name, app_alias)
app_object_method_template = '''
  JSON {method_name}(const std::string &app_id_or_name, const std::string &input_params="{{}}", const bool safe_to_retry={to_retry});
  JSON {method_name}(const std::string &app_id_or_name, const dx::JSON &input_params, const bool safe_to_retry={to_retry});
  JSON {method_name}WithAlias(const std::string &app_name, const std::string &app_alias, const std::string &input_params="{{}}", const bool safe_to_retry={to_retry});
  JSON {method_name}WithAlias(const std::string &app_name, const std::string &app_alias, const dx::JSON &input_params, const bool safe_to_retry={to_retry});'''

print preamble

for method in json.loads(sys.stdin.read()):
    route, signature, opts = method
    method_name = signature.split("(")[0]
    retry = "true" if (opts['retryable']) else "false"
    if (opts['objectMethod']):
        root, oid_route, method_route = route.split("/")
        if oid_route == 'app-xxxx':
            print app_object_method_template.format(method_name=method_name, to_retry=retry, method_route=method_route)
        else:
            print object_method_template.format(method_name=method_name, to_retry=retry, method_route=method_route)
    else:
        print class_method_template.format(method_name=method_name, to_retry=retry, route=route)

print postscript
